OneHot

根据输入的索引值,在指定轴上生成独热编码(one-hot encoding)。

\[\begin{split}output_{i_1, i_2, \dots, i_{axis}, \dots, i_n} = \begin{cases} on\_value, & \text{if } i_{axis} = indices_{j} \\ off\_value, & \text{otherwise} \end{cases}\end{split}\]

其中,\(indices_{j}\) 为输入索引张量的元素,索引 \(j\) 与输出下标 \((i_1, \dots, i_{axis-1}, i_{axis+1}, \dots, i_n)\) 一一对应。 \(depth\) 表示 one-hot 向量的长度,\(axis\) 表示新维度插入的位置。

当启用 support_neg_index 时,若 \(indices_{j} < 0\),则执行如下修正:

\[indices_{j} = indices_{j} + depth\]
输入:
  • on_off - 包含 on_valueoff_value 的数组地址。对于标量数据类型,数组长度为 2,依次为 [on_value, off_value];对于复数类型(c64/c128),数组长度为 4,依次为 [on_real, on_imag, off_real, off_imag]

  • indices - 输入索引值地址,索引元素类型为 int

  • params - 参数数组地址,依次存放以下 5 个元素:

    • params[0]axis,指定生成 one-hot 编码的轴。

    • params[1]depth,独热编码向量的长度。

    • params[2]support_neg_index,是否支持负索引,取值为 0(不支持)或 1(支持)。

    • params[3]indices_shape_size,输入索引张量的维度数(即 indices_shape 数组的长度)。

    • params[4]indices_shape,输入索引张量各维度大小的数组地址。

  • core_mask - 核心掩码,指定使用的计算核心(仅共享存储版本需要)。

输出:
  • output - 生成的独热编码结果地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持int8, int16, int32, fp32, fp64, cplx64, cplx128

  • MT7004 支持fp16, fp32, int16, int32, cplx64

  • 本算子通过 params 数组将 axisdepthsupport_neg_indexindices_shape_sizeindices_shape 打包传入。

共享存储版本:

void i8_one_hot_s(int8_t *on_off, int *indices, int8_t *output, long long *params, int core_mask)
void i16_one_hot_s(int16_t *on_off, int *indices, int16_t *output, long long *params, int core_mask)
void i32_one_hot_s(int *on_off, int *indices, int *output, long long *params, int core_mask)
void hp_one_hot_s(half *on_off, int *indices, half *output, long long *params, int core_mask)
void fp_one_hot_s(float *on_off, int *indices, float *output, long long *params, int core_mask)
void dp_one_hot_s(double *on_off, int *indices, double *output, long long *params, int core_mask)
void c64_one_hot_s(float *on_off, int *indices, float *output, long long *params, int core_mask)
void c128_one_hot_s(double *on_off, int *indices, double *output, long long *params, int core_mask)

C调用示例:

 1//MT7004示例
 2#include <stdio.h>
 3
 4int main(int argc, char* argv[]) {
 5    float on_off[2] = {1.0, 0.0};
 6    int axis=1, depth=4, indices_shape_size=2;
 7    int support_neg_index=1;
 8    int indices_shape[2] = {3, 3};
 9    long long params[5] = {axis, depth, support_neg_index, indices_shape_size, (long long)indices_shape};
10    int *indices = (int *)0xA0000000; // indices在DDR空间
11    float *output = (float*)0xB0000000; // indices_shape[0] * depth * indices_shape[1]
12    int core_mask = 0xff;
13    fp_one_hot_s(on_off, indices, output, params, core_mask);
14    return 0;
15}

私有存储版本:

void i8_one_hot_p(int8_t *on_off, int *indices, int8_t *output, long long *params)
void i16_one_hot_p(int16_t *on_off, int *indices, int16_t *output, long long *params)
void i32_one_hot_p(int *on_off, int *indices, int *output, long long *params)
void hp_one_hot_p(half *on_off, int *indices, half *output, long long *params)
void fp_one_hot_p(float *on_off, int *indices, float *output, long long *params)
void dp_one_hot_p(double *on_off, int *indices, double *output, long long *params)
void c64_one_hot_p(float *on_off, int *indices, float *output, long long *params)
void c128_one_hot_p(double *on_off, int *indices, double *output, long long *params)

C调用示例:

 1//MT7004示例
 2#include <stdio.h>
 3
 4int main(int argc, char* argv[]) {
 5    float on_off[2] = {1.0, 0.0};
 6    int axis=1, depth=4, indices_shape_size=2;
 7    int support_neg_index=1;
 8    int indices_shape[2] = {3, 3};
 9    long long params[5] = {axis, depth, support_neg_index, indices_shape_size, (long long)indices_shape};
10    int *indices = (int *)0x10000000; // indices在L2空间
11    float *output = (float*)0x10001000; // indices_shape[0] * depth * indices_shape[1]
12    fp_one_hot_p(on_off, indices, output, params);
13    return 0;
14}